package com.lgt_enterprise_android.view.pages.b;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.CommonUtil;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.StringUtil;

public class InviteStaffActivity extends BaseActivity {

    LinearLayout mLinContainer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invite_staff);
        initView();
        bindEvent();
    }


    protected void initView() {
        mLinContainer = (LinearLayout) findViewById(R.id.lin_container);
        addInputView();

        findViewById(R.id.customizeTitleView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                JSONArray jsonArray = new JSONArray();

                if(mLinContainer.getChildCount()==1){
                    showToast("邀请的数据不能为空");
                    return;
                }

                for (int i = 0; i < mLinContainer.getChildCount()-1; i++) {
                    View viewLin = mLinContainer.getChildAt(i);

                    EditText edName = viewLin.findViewById(R.id.ed_name);
                    EditText edPhone = viewLin.findViewById(R.id.ed_phone);

                    String strName = edName.getText().toString().trim();
                    String strPhone = edPhone.getText().toString().trim();

                    if (StringUtil.isEmpty(strName) && StringUtil.isEmpty(strPhone)) {
                        continue;
                    }


                    if (StringUtil.isEmpty(strName)) {
                        TextView viewTipName = (TextView) viewLin.findViewById(R.id.txt_tip_name);
                        viewTipName.setText("姓名不能为空");
                        viewTipName.setVisibility(View.VISIBLE);
                        return;
                    }

                    TextView viewTip = (TextView) viewLin.findViewById(R.id.txt_tip_phone);

                    if (StringUtil.isEmpty(strPhone)) {
                        viewTip.setText("手机号不能为空");
                        viewTip.setVisibility(View.VISIBLE);
                        return;
                    }

                    if (!CommonUtil.isPhoneNumValid(strPhone)) {
                        viewTip.setText("手机号格式不正确");
                        viewTip.setVisibility(View.VISIBLE);
                        return;
                    }

                    JSONObject tmp = new JSONObject();
                    tmp.put("name", strName);
                    tmp.put("phoneNumber",strPhone);

                    jsonArray.add(tmp);
                }

                if(jsonArray.size()==0){
                    showToast("邀请的数据不能为空");
                    return;
                }else{
                    HttpManager.inviteStaff(mContext, jsonArray, new BasicHttp.IMyCallBack() {
                        @Override
                        public void onResponse(Object resonseObj) {
                            showToast("邀请成功");
                            finish();
                        }

                        @Override
                        public void onFailure(String mes) {

                        }
                    });
                }
            }
        });

    }

    protected void bindEvent() {
        findViewById(R.id.rel_add_staff).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View lastInput = mLinContainer.getChildAt(mLinContainer.getChildCount() - 2);

                EditText edName = lastInput.findViewById(R.id.ed_name);
                EditText edPhone = lastInput.findViewById(R.id.ed_phone);
                final TextView viewTip = (TextView) lastInput.findViewById(R.id.txt_tip_phone);
                final TextView viewTipName = (TextView) lastInput.findViewById(R.id.txt_tip_name);

                edPhone.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        viewTip.setVisibility(View.GONE);
                    }
                });

                edName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        viewTipName.setVisibility(View.GONE);
                    }
                });

                String strName = edName.getText().toString();
                String strPhone = edPhone.getText().toString();

                if (StringUtil.isEmpty(strName)) {
                    viewTipName.setText("姓名不能为空");
                    viewTipName.setVisibility(View.VISIBLE);
                    return;
                }

                if (StringUtil.isEmpty(strPhone)) {
                    viewTip.setText("手机号不能为空");
                    viewTip.setVisibility(View.VISIBLE);
                    return;
                }

                if (!CommonUtil.isPhoneNumValid(strPhone)) {
                    viewTip.setText("手机号格式不正确");
                    viewTip.setVisibility(View.VISIBLE);
                    return;
                }
                addInputView();

            }
        });
    }

    private void addInputView() {
        View view = getLayoutInflater().inflate(R.layout.item_invite_staff_edit, null);
        mLinContainer.addView(view, mLinContainer.getChildCount() - 1);
    }
}
